package com.example.jpetstoremanager.controller;

import com.alibaba.fastjson.JSON;
import com.example.jpetstoremanager.entity.Account;
import com.example.jpetstoremanager.entity.User;
import com.example.jpetstoremanager.service.AccountService;
import com.example.jpetstoremanager.service.UserService;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class UserManageController {


    @Autowired
    private UserService userService;

    @Autowired
    private AccountService accountService;


    @GetMapping("/userManage")
    public String userManage(HttpSession session){
        //首页用户信息显示所有用户
        List<User> userList = userService.findAllUsers();
        session.setAttribute("userList",userList);
        return "userManage";
    }

    @PostMapping("/addUser")
    public String addUser(@Validated @ModelAttribute User user,BindingResult bindingResult,HttpSession session){
        //1、用户名、密码输入格式不正确
        StringBuilder validationErrorMsg = new StringBuilder();
        if(bindingResult.hasErrors()){
            for (ObjectError error: bindingResult.getAllErrors()){
                validationErrorMsg.append(error.getDefaultMessage());
                validationErrorMsg.append(",");
            }
            session.setAttribute("addUserMsg",validationErrorMsg.subSequence(0,validationErrorMsg.length()-1));
        }
        //id重复
        else if (userService.IsUserExist(user.getId())){
            session.setAttribute("addUserMsg","该编号用户已存在！");
        }
        else{
            userService.insertUser(user);
//            Account account = new Account();
//            account.setUsername(user.getUsername());
//            account.setEmail(user.getEmail());
//            accountService.insertAccount(account);
            session.setAttribute("addUserMsg","用户添加成功");
            List<User> userList = userService.findAllUsers();
            session.setAttribute("userList",userList);
        }
        System.out.println(session.getAttribute("addUserMsg"));
        return "redirect:/userManage";
    }

    @PostMapping("/editUser")
    public String editUser(@Validated @ModelAttribute User user,BindingResult bindingResult,HttpSession session){
        System.out.println(user);
        //用户名、密码输入格式不正确
        StringBuilder validationErrorMsg = new StringBuilder();
        if(bindingResult.hasErrors()){
            for (ObjectError error: bindingResult.getAllErrors()){
                validationErrorMsg.append(error.getDefaultMessage());
                validationErrorMsg.append(",");
            }
            session.setAttribute("editUserMsg",validationErrorMsg.subSequence(0,validationErrorMsg.length()-1));
        }
            userService.updateUserById(user);
            System.out.println("用户："+user.getUsername()+" 信息修改成功......");
            List<User> userList = userService.findAllUsers();
            session.setAttribute("userList",userList);

        return "redirect:/userManage";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(@RequestParam("userId") int userId,HttpSession session){
        userService.deleteUserById(userId);
        System.out.println("用户ID:"+userId+" 删除成功.......");
        List<User> userList = userService.findAllUsers();
        session.setAttribute("userList",userList);
        return "redirect:/userManage";
    }
//
//    @GetMapping("/userinfo")
//    public String userinfo(@RequestParam("username") String username,HttpSession session){
//        Account account = accountService.getAccount(username);
//        session.setAttribute("account",account);
//        System.out.println("查看用户："+username+" 的详细信息......");
//        return "redirect:/userManage";
//    }

}
